package org.jiamandan.com.strategy.impl.tram.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.OrderDto;
import org.jiamandan.com.dto.ZDBasicDto;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.strategy.impl.tram.IZDListenerService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-12-12 14:00
 * @功能说明： 嘉满电-YD充电时功率心跳包

 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	        CWJ       	创建
 * @end
 */
@Service(value = BizConstant.CmdClass.CMD_YD_06_SERVICE)
@AllArgsConstructor
@Slf4j
public class YDChargeRecordHeartListenerService implements IZDListenerService {

    @Override
    public void doExecute(ZDBasicDto dto, Channel client) throws Exception {
        String deviceNo = StrUtil.toString(Integer.parseInt(CommonUtil.reverseHex(dto.getDeviceNo()).substring(2), 16));
        log.info("接收嘉满电-YD端口充电时功率心跳包上传(CMD=06)【" + deviceNo + "】报文:" + JSON.toJSONString(dto));
        String data = dto.getData();
        String gunNo = StringUtils.substring(data, 0, 2); //00表示1号端口，01表示2号端口
        String workStatus = StringUtils.substring(data, 2, 4); //1=充电中， 5=浮充
        log.info("(CMD=06)1=充电中,5=浮充:"+workStatus);
        Integer sumChargeTime = Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 4, 8)), 16);
        BigDecimal totalElectric = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 8, 12)), 16)).divide(BigDecimal.valueOf(100));
        Integer startChargeType = Integer.parseInt(StringUtils.substring(data, 12, 14), 16);
        BigDecimal realTimepower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 14, 18)),16)).divide(BigDecimal.valueOf(10));
        BigDecimal maxpower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 18, 22)),16)).divide(BigDecimal.valueOf(10));
        BigDecimal minpower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 22, 26)),16)).divide(BigDecimal.valueOf(10));
        BigDecimal averagePower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 26, 30)),16)).divide(BigDecimal.valueOf(10));
        String tradeNo = CommonUtil.removeZero(StringUtils.substring(data, 30, 62), true);
        String currentElectric = StringUtils.substring(data, 62, 66);
        BigDecimal peakPower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 66, 70)),16)).divide(BigDecimal.valueOf(10));
        log.info("(CMD=06)【" + deviceNo +"插口"+ gunNo+"】:实时功率:"+realTimepower+",最大功率:"+maxpower+",峰值功率:"+peakPower);
        BigDecimal vol = new BigDecimal(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 70, 74)),16)).divide(BigDecimal.valueOf(10));
        BigDecimal cul = new BigDecimal(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 74, 78)),16)).divide(BigDecimal.valueOf(1000));
        int eTemp = Integer.parseInt(StringUtils.substring(data, 78, 80), 16);
        int chargePortTemp = Integer.parseInt(StringUtils.substring(data, 80, 82), 16);
        log.info("(CMD=06)当前设备【" + deviceNo +"插口"+ gunNo+"】:环境温度:"+StrUtil.toString(eTemp-65)+"端口温度:"+chargePortTemp);
        //更新充电时间
        //根据订单tradeNo 获取
        OrderDto dto1 = getOrderDto(tradeNo);
        issueChargeTime(dto1,client);
    }
    OrderDto getOrderDto(String tradeNo){
        return new OrderDto();
    }
    private void issueChargeTime(OrderDto dto,Channel client) {
        RechargePile pile = new RechargePile();
        pile.setCode(4);
        pile.setPileNo(dto.getPileNo());
        Integer maxChargeTime = 36000; //最大充电时间 10小时,根据选择的金额进行计算
        StringBuilder payload = new StringBuilder();
        payload.append("01")
                .append(dto.getGunNo())
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(maxChargeTime),4,"0")));
        String replyMsg = ZDObjectUtil.ydsplicingMessage(payload.toString(), pile,dto.getGunNo(), BizConstant.Cmd.CMD_YD_8a);
        log.info("嘉满电-YD下发服务器设备号【"+dto.getPileNo()+","+dto.getGunNo()+"】修改充电时长/电量:"+maxChargeTime+"秒，功率："+dto.getMaxPower()+"(CMD=8a)报文:" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
    }

    public static String secondConvertHourMinSecond(Integer second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        System.out.println(h);
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }
}
